package com.jianjun.project.system.dict;

import cn.hutool.core.convert.Convert;
import cn.hutool.http.Method;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jianjun.common.constant.HttpStatus;
import com.jianjun.common.util.SecurityUtils;
import com.jianjun.framework.web.domain.AjaxResult;
import com.jianjun.framework.web.interceptor.DataScope;
import com.jianjun.framework.web.page.TableDataInfo;
import com.jianjun.framework.web.render.ExcelWriterRender;
import com.jianjun.project.common.model.SysDictData;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 *
 * @author source
 * {@code @date} 2025/1/9 15:21
 */
@Path(value = "/system/dict/data")
public class SysDictDataController extends Controller {
    @Inject
    SysDictDataService dictDataService;

    @DataScope(permission = "system:dict:list")
    public void list() {
        SysDictData dictData = JsonKit.parse(getKv().toJson(), SysDictData.class);
        Page<SysDictData> page = dictDataService.paginate(dictData);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getList());
        rspData.setTotal(page.getTotalRow());
        renderJson(rspData);
    }

    @DataScope(permission = "system:dict:export")
    public void export() {
        SysDictData dictData = JsonKit.parse(getKv().toJson(), SysDictData.class);
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        List<Map<String, Object>> mapList = list.stream().map(Model::toMap).collect(Collectors.toList());
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.write(mapList, true);
        render(new ExcelWriterRender(writer, dictData.getClass().getSimpleName()));
    }

    @DataScope(permission = "system:dict:query,system:dict:add,system:dict:edit,system:dict:remove")
    public void index() {
        String method = getRequest().getMethod();
        //查询
        if (Method.GET.name().equals(method)) {
            Long dictCode = getParaToLong();
            renderJson(AjaxResult.success(dictDataService.selectDictDataById(dictCode)));
        }
        //新增
        if (Method.POST.name().equals(method)) {
            SysDictData dict = JsonKit.parse(getRawData(), SysDictData.class);
            dict.setCreateBy(SecurityUtils.getUsername());
            renderJson(AjaxResult.success(dictDataService.insertDictData(dict)));
        }
        //修改
        if (Method.PUT.name().equals(method)) {
            SysDictData dict = JsonKit.parse(getRawData(), SysDictData.class);
            dict.setUpdateBy(SecurityUtils.getUsername());
            renderJson(AjaxResult.success(dictDataService.updateDictData(dict)));
        }
        //删除
        if (Method.DELETE.name().equals(method)) {
            dictDataService.deleteDictDataByIds(Convert.toLongArray(getPara()));
            renderJson(AjaxResult.success());
        }
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    public void type() {
        String dictType = getPara();
        List<SysDictData> data = dictDataService.selectDictDataByType(dictType);
        if (Objects.isNull(data)) {
            data = new ArrayList<>();
        }
        renderJson(AjaxResult.success(data));
    }
}
